package com.lagou.controller;

import com.lagou.model.Result;
import com.lagou.model.ResultStatus;
import com.lagou.service.IUserService;
import com.lagou.pojo.Token;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
/**
 * <p>Title: UserController</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2020</p>
 * <p>Company: http://www.ubisys.com.cn/</p>
 *
 * @Auther: cw
 * @Date: 2020/7/22 16:30
 */
@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Reference
    private IUserService userService;

    @Value("${config.cookie.name.auth}")
    private String cookieTag;

    /**
     * 注册
     *
     * @param response
     * @param email
     * @param password
     * @param code
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public Result register(HttpServletResponse response,
                           @PathVariable("email") String email,
                           @PathVariable("password") String password,
                           @PathVariable("code") String code) {
        log.info(" register email:{}. password:{}. code:{} ", email, password, code);
        Result token = userService.register(email, password, code);
        return token;
    }

    /**
     * 检查邮箱是否已注册
     *
     * @param email
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public boolean checkRegistered(@PathVariable("email") String email) {
        log.info("  checkRegistered  email:{} ", email);
        return userService.isRegistered(email);
    }

    /**
     * 使用  邮箱  +  密码登录
     *
     * @param response
     * @param email
     * @param password
     * @return
     */
    @GetMapping("/login/{email}/{password}")
    public Result login(HttpServletResponse response,
                        @PathVariable String email,
                        @PathVariable String password) {
        log.info("login email:{}  password:{}", email, password);
        Result result = userService.login(email, password);
        if (Result.isOK(result)) {
            response.addHeader(cookieTag, result.getData().toString());
        }
        return result;
    }


    @GetMapping("/info/{token}")
    public Result getEmailByToken(@PathVariable String token) {
        log.info("getEmailByToken token:{} ", token);
        String email = userService.getEmailByToken(token);
        if (StringUtils.isNotBlank(email)) {
            return Result.ok(email);
        }
        return Result.build(ResultStatus.ERROR_SYSTEM, "好像木有这个邮箱");
    }


    /**
     * 退出登录
     *
     */
    @GetMapping("/logout")
    public Result logout() {
        log.info("logout");
        return Result.ok();
    }
}
